package com.hbnrtech.sip.gb28181.deivce;

import com.hbnrtech.sip.gb28181.config.SipConfigProperties;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.Instant;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author liuduo
 * @date 2025-04-21 10:13
 */
@Component
public class DeviceRegistry {

    @Autowired
    private SipConfigProperties sipConfigProperties;

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class DeviceInfo {
        private String deviceId;
        private String ip;
        private int port;
        private String transport; // UDP or TCP
        private Instant registrationTime;
        private Instant expiryTime;
    }

    // 使用 ConcurrentHashMap 保证线程安全
    private final Map<String, DeviceInfo> registeredDevices = new ConcurrentHashMap<>();

    public void registerDevice(DeviceInfo deviceInfo) {
        registeredDevices.put(deviceInfo.getDeviceId(), deviceInfo);
    }

    public void unregisterDevice(String deviceId) {
        registeredDevices.remove(deviceId);
    }

    public DeviceInfo getDevice(String deviceId) {
        return registeredDevices.get(deviceId);
    }

    public Map<String, DeviceInfo> getAllDevices() {
        return registeredDevices;
    }

    // 实际应用中可能需要根据密码查询等
    public String getPasswordForDevice(String deviceId) {
        return sipConfigProperties.getPassword();
    }
}